<?php

/**
 * Description of SliderController
 * 
 * @author anhducbkhn
 * @since Oct 14, 2013 - 11:47:23 AM
 */
class SliderController extends MyController
{

    public function actionIndex()
    {
        $this->checkAccess();
        //Pagination
        $cri             = new CDbCriteria();
        $cri->order      = 'rank DESC';
        $count           = Slider::model()->count($cri);
        $pages           = new CPagination($count);
        $pages->route    = "slider/index";
        $pages->pageSize = $this->currentLimit;
        $pages->applyLimit($cri);
        $data            = Slider::model()->findAll($cri);

        $this->render('index', array(
            'model' => $data,
            'pages' => $pages
        ));
    }

    public function actionAdd()
    {
        $this->checkAccess();
        $model = new Slider();
        if (isset($_POST['Slider']))
        {
            $model->attributes = $_POST['Slider'];
            $firstAvarta       = $this->_uploadPhoto('service', 1920, 800, 'SliderImages');

            if ($firstAvarta && !$model->images)
            {
                $model->images = $firstAvarta;
            }
            if ($model->validate() && $model->save())
            {
                Yii::app()->user->setFlash('success', 'Item added!');
                $this->redirect(array('slider/index'));
            }
            else
            {
                Yii::app()->user->setFlash('error', 'Error!');
            }
        }

        $this->render('_form', array(
            'model' => $model
        ));
    }

    public function actionEdit()
    {
        if (!isset($_GET['pk']))
        {
            throw new CHttpException(404, Yii::t('yii', 'Invalid request!'));
        }
        $model = Slider::model()->findByPk($_GET['pk']);
        if (isset($_POST['Slider']))
        {
            $model->attributes = $_POST['Slider'];
            $firstAvarta       = $this->_uploadPhoto('service', 1920, 800, 'SliderImages');

            if ($firstAvarta)
            {
                $model->images = $firstAvarta;
            }

            if ($model->validate() && $model->save())
            {
                Yii::app()->user->setFlash('success', 'Item edited!');
                $this->redirect(array('slider/index'));
            }
            else
            {
                Yii::app()->user->setFlash('error', 'Error!');
            }
        }
        $this->render('_form', array(
            'model' => $model
        ));
    }

    public function actionDelete($pk)
    {
        $this->checkAccess();
        if (isset($_GET['pk']))
        {
            if (Slider::model()->deleteByPk($pk))
            {
                Yii::app()->user->setFlash('success', 'Delete is successfuly!');
            }
            else
            {
                Yii::app()->user->setFlash('error', 'Can\'t delete this item, please try again!');
            }
            $this->redirect($_SERVER['HTTP_REFERER']);
        }
    }

    protected function _uploadPhoto($dir, $size_w = 320, $size_h = 320, $name = 'file')
    {
        if (@$_FILES[$name])
        {
            $file     = $_FILES[$name];
            $tmp_file = "files/$dir/" . md5(time() . $file['name']) . "_" . $file['name'];
            if (move_uploaded_file($file['tmp_name'], $tmp_file))
            {
                $thumb = Yii::app()->phpThumb->create($tmp_file);
                $thumb->adaptiveResize($size_w, $size_h);
                $thumb->save($tmp_file);
                return $tmp_file;
            }
            else
            {
                return FALSE;
            }
        }

        return FALSE;
    }

}

/**
     * End of SliderController
     */
